using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using Common;
using Common.Command;
using Common.Enum;
using Common.NotifyEvent;
using SSettings;

namespace SettingsView.ViewModel
{
	public class GridColumnEditViewMode : NotifyPropertyChanged
	{
		private ColumnInfo _SelectedColumn;

		private EColumnType _Type;

		private string _SelectedItem;

		private ICommand _CommandAddColumn;

		private ICommand _CommandDeleteColumn;

		private ICommand _CommandUp;

		private ICommand _CommandDown;

		private ICommand _CommandAddItem;

		private ICommand _CommandDeleteItem;

		private ICommand _CommandRestoreDefaultItem;

		private ICommand _CommandRestoreDefaultSelect;

		private GridColumns _GridColumns;

		public List<KeyValuePair> ListColumnType
		{
			get
			{
				return EnumManagement.GetColumnTypeList();
			}
		}

		public ObservableCollection<ColumnInfo> Columns
		{
			get
			{
				if (_GridColumns == null)
				{
					return null;
				}
				return _GridColumns.Columns;
			}
		}

		public ColumnInfo SelectedColumn
		{
			get
			{
				return _SelectedColumn;
			}
			set
			{
				if (_SelectedColumn != value)
				{
					_SelectedColumn = value;
					OnSelectedColumnChannged();
					OnPropertyChanged("SelectedColumn");
				}
			}
		}

		public EColumnType Type
		{
			get
			{
				return _Type;
			}
			set
			{
				if (_Type != value)
				{
					_Type = value;
					OnSelectedItemChanged();
					OnPropertyChanged("Type");
				}
			}
		}

		public string SelectedItem
		{
			get
			{
				return _SelectedItem;
			}
			set
			{
				if (!(_SelectedItem == value))
				{
					_SelectedItem = value;
					OnSelectedItemChanged();
					OnPropertyChanged("SelectedItem");
				}
			}
		}

		public ICommand CommandAddColumn
		{
			get
			{
				if (_CommandAddColumn == null)
				{
					_CommandAddColumn = new SimpleDelegateCommand(CommandAddColumnExecute);
				}
				return _CommandAddColumn;
			}
		}

		public ICommand CommandDeleteColumn
		{
			get
			{
				if (_CommandDeleteColumn == null)
				{
					_CommandDeleteColumn = new SimpleDelegateCommand(CommandDeleteColumnExecute, CommandDeleteColumnCanExecute);
				}
				return _CommandDeleteColumn;
			}
		}

		public ICommand CommandUp
		{
			get
			{
				if (_CommandUp == null)
				{
					_CommandUp = new SimpleDelegateCommand(CommandUpExecute, CommandUpCanExecute);
				}
				return _CommandUp;
			}
		}

		public ICommand CommandDown
		{
			get
			{
				if (_CommandDown == null)
				{
					_CommandDown = new SimpleDelegateCommand(CommandDownExecute, CommandDownCanExecute);
				}
				return _CommandDown;
			}
		}

		public ICommand CommandAddItem
		{
			get
			{
				if (_CommandAddItem == null)
				{
					_CommandAddItem = new SimpleDelegateCommand(CommandAddItemExecute, CommandAddItemCanExecute);
				}
				return _CommandAddItem;
			}
		}

		public ICommand CommandDeleteItem
		{
			get
			{
				if (_CommandDeleteItem == null)
				{
					_CommandDeleteItem = new SimpleDelegateCommand(CommandDeleteItemExecute, CommandDeleteItemCanExecute);
				}
				return _CommandDeleteItem;
			}
		}

		public ICommand CommandRestoreDefaultItem
		{
			get
			{
				if (_CommandRestoreDefaultItem == null)
				{
					_CommandRestoreDefaultItem = new SimpleDelegateCommand(CommandRestoreDefaultItemExecute, CommandRestoreDefaultItemCanExecute);
				}
				return _CommandRestoreDefaultItem;
			}
		}

		public ICommand CommandRestoreDefaultSelect
		{
			get
			{
				if (_CommandRestoreDefaultSelect == null)
				{
					_CommandRestoreDefaultSelect = new SimpleDelegateCommand(CommandRestoreDefaultSelectExecute, CommandRestoreDefaultSelectCanExecute);
				}
				return _CommandRestoreDefaultSelect;
			}
		}

		public GridColumnEditViewMode(GridColumns gridColumns)
		{
			_GridColumns = gridColumns;
			if (_GridColumns.Columns.Count > 0)
			{
				SelectedColumn = _GridColumns.Columns[0];
			}
		}

		public void CommandAddColumnExecute()
		{
			if (_GridColumns != null)
			{
				ColumnInfo columnInfo = new ColumnInfo();
				_GridColumns.Columns.Add(columnInfo);
				SelectedColumn = columnInfo;
			}
		}

		public bool CommandDeleteColumnCanExecute()
		{
			if (SelectedColumn == null)
			{
				return false;
			}
			return SelectedColumn.AllowDelete;
		}

		public void CommandDeleteColumnExecute()
		{
			if (SelectedColumn != null && MessageBox.Show(Utility.GetMessage("msg_delete_column_confirm"), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
			{
				int num = Columns.IndexOf(SelectedColumn);
				Columns.Remove(SelectedColumn);
				if (Columns.Count > 0)
				{
					SelectedColumn = ((num >= Columns.Count) ? Columns[num - 1] : Columns[num]);
				}
				else
				{
					SelectedColumn = null;
				}
			}
		}

		public bool CommandDownCanExecute()
		{
			if (SelectedColumn == null)
			{
				return false;
			}
			if (!SelectedColumn.Display)
			{
				return false;
			}
			int num = Columns.Where((ColumnInfo s) => s.Display).ToList().IndexOf(SelectedColumn);
			return num < Columns.Where((ColumnInfo s) => s.Display).ToList().Count - 1;
		}

		public void CommandDownExecute()
		{
			if (SelectedColumn != null)
			{
				int num = Columns.IndexOf(SelectedColumn);
				if (num < Columns.Count - 1)
				{
					Columns.Move(num, num + 1);
					OnSelectedColumnChannged();
				}
			}
		}

		public bool CommandUpCanExecute()
		{
			if (SelectedColumn == null)
			{
				return false;
			}
			if (!SelectedColumn.Display)
			{
				return false;
			}
			int num = Columns.Where((ColumnInfo s) => s.Display).ToList().IndexOf(SelectedColumn);
			return num > 0;
		}

		public void CommandUpExecute()
		{
			if (SelectedColumn != null)
			{
				int num = Columns.IndexOf(SelectedColumn);
				if (num > 0)
				{
					Columns.Move(num, num - 1);
					OnSelectedColumnChannged();
				}
			}
		}

		public bool CommandAddItemCanExecute()
		{
			if (SelectedColumn == null)
			{
				return false;
			}
			return SelectedColumn.Type == EColumnType.ComboBox;
		}

		public void CommandAddItemExecute()
		{
			if (SelectedColumn != null)
			{
				NewNameWindow newNameWindow = new NewNameWindow();
				newNameWindow.Owner = Utility.GetMainWindow();
				newNameWindow.Items = SelectedColumn.DataSource;
				if (newNameWindow.ShowDialog() == true)
				{
					SelectedColumn.DataSource.Add(newNameWindow.GetNewName());
				}
			}
		}

		public bool CommandDeleteItemCanExecute()
		{
			return SelectedItem != null;
		}

		public void CommandDeleteItemExecute()
		{
			if (SelectedColumn != null && SelectedItem != null)
			{
				int num = SelectedColumn.DataSource.IndexOf(SelectedItem);
				SelectedColumn.DataSource.Remove(SelectedItem);
				if (SelectedColumn.DataSource.Count > 0)
				{
					SelectedItem = ((num >= SelectedColumn.DataSource.Count) ? SelectedColumn.DataSource[num - 1] : SelectedColumn.DataSource[num]);
				}
				else
				{
					SelectedItem = null;
				}
			}
		}

		public bool CommandRestoreDefaultItemCanExecute()
		{
			if (SelectedColumn == null)
			{
				return false;
			}
			return SelectedColumn.AllowDelete;
		}

		public void CommandRestoreDefaultItemExecute()
		{
			if (SelectedColumn != null)
			{
				GridColumns girdColumnsCommonByColumn = ConfigSettings.GetInstance().GetGirdColumnsCommonByColumn(_GridColumns);
				IList<ColumnInfo> list = girdColumnsCommonByColumn.Columns.Where((ColumnInfo s) => s.FieldName == SelectedColumn.FieldName).ToList();
				if (list.Count == 1)
				{
					SelectedColumn.DataSource = new ObservableCollection<string>(list[0].DataSource);
				}
			}
		}

		public bool CommandRestoreDefaultSelectCanExecute()
		{
			return true;
		}

		public void CommandRestoreDefaultSelectExecute()
		{
			ConfigSettings.GetInstance().RestoreGirdColumnsCommonByColumn(_GridColumns);
			_GridColumns = ConfigSettings.GetInstance().GetGirdColumnsCommonByColumn(_GridColumns);
			OnPropertyChanged("Columns");
		}

		private void OnSelectedColumnChannged()
		{
			((SimpleDelegateCommand)CommandDown).RaiseCanExecuteChanged();
			((SimpleDelegateCommand)CommandUp).RaiseCanExecuteChanged();
			((SimpleDelegateCommand)CommandDeleteColumn).RaiseCanExecuteChanged();
			if (SelectedColumn != null)
			{
				Type = SelectedColumn.Type;
			}
		}

		private void OnSelectedItemChanged()
		{
			if (SelectedColumn != null)
			{
				SelectedColumn.Type = Type;
			}
			((SimpleDelegateCommand)CommandAddItem).RaiseCanExecuteChanged();
			((SimpleDelegateCommand)CommandDeleteItem).RaiseCanExecuteChanged();
		}
	}
}
